<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * ModerationController
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class ModerationController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// auth
		$auth = Zend_Auth::getInstance();
    	$this->view->auth = $auth->hasIdentity();
    	
    	if($auth->hasIdentity())
    	{
    		$this->view->auth_role 			= $auth->getIdentity()->role;
    		$this->view->username 			= $auth->getIdentity()->username;
    		$this->view->auth_uid			= $auth->getIdentity()->uid;
    		$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
    	}
    	else
    	{
    		$this->view->username 	= 'Gast';
    	}

    	$this->auth = $auth;
    	
    	// lang
		$this->view->translate = $this->registry->get('lang');
		
		// flash messenger
		$this->view->flashMessenger	= $this->_helper->FlashMessenger;
		
		// include jquery
		$baseurl = new MHDEV_View_Helper_BaseUrl();
		$this->view->headScript()->appendFile($baseurl->BaseUrl() . '/static/css/default/jquery.css');
		$this->view->headScript()->appendFile($baseurl->BaseUrl() . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($baseurl->BaseUrl() . '/static/js/jquery-ui-1.7.2.custom.min.js');
		$this->view->headScript()->appendFile($baseurl->BaseUrl() . '/static/js/date.js');
		$this->view->headScript()->appendFile($baseurl->BaseUrl() . '/static/js/jquery.datePicker.js');
	}
	
	
	
	/*
	 * Create a new ban
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function banAction()
	{
		// params
		$uid 		= intval($this->getRequest()->getParam('uid', 0));
		
		// vars
		$postData 	= $this->_request->getPost();
		
		// helper
		$author		= new MHDEV_View_Helper_GetAuthor();
		$pm			= new MHDEV_View_Helper_SendMessage();
		$date		= new MHDEV_View_Helper_Datetime();
		
		// form
		$form 		= new BanForm();
		
		// db
		$posts	 	= new Posts();
		$threads	= new Threads();
		$boards		= new Boards();
		$bans		= new Bans();
		
		// query
		$getPost	= $posts->find($uid);
		
		if($getPost->count() > 0)
		{
			$resPost	= $getPost->toArray();
			
			$getThread	= $threads->find($resPost[0]['thread_uid']);
			$resThread	= $getThread->toArray();
			
			$getBoard	= $boards->find($resThread[0]['board_uid']);
			$resBoard	= $getBoard->toArray();
			
			$notice 	= MHDEV_Sprintf($this->registry->get('lang')->_('ban_notice'), array(
				'%user'		=> $author->GetAuthor($resPost[0]['user_uid']),
				'%thread'	=> $resThread[0]['title'],
			));
			
			$form->getElement('user_uid')->setValue($resPost[0]['user_uid']);
			$form->getElement('thread_uid')->setValue($resThread[0]['uid']);
			
			// assign boards to form
			$getBoard 	= $form->getElement('board_uid');
			if($boards->fetchAll()->count() > 0)
			{
				foreach($boards->fetchAll()->toArray() AS $result)
				{
					$getBoard->addMultiOption($result['uid'], $result['title']);
				}
			}
			
			// get post		
			if ($this->_request->isPost() && $form->isValid($postData))
			{
				$form->removeElement('submit');
				
				if($ban_uid = $bans->insert($form->getValues()))
				{
					$bans->update(array(
						'create_date' 	=> new Zend_Db_Expr('NOW()'),
						'admin_uid'		=> $this->auth->getIdentity()->uid,
						'post_uid'		=> $uid
					), 'uid =' . $ban_uid);
					
					// log action
					$this->registry->get('log')->info('[MODERATE] User with ID ' . $resPost[0]['user_uid'] . ' is now banned. Post-ID: ' . $uid . ', Admin: ' . $this->auth->getIdentity()->uid);
					
					// notify user
					$pm->SendMessage($resPost[0]['user_uid'], $this->registry->get('lang')->_('banned_pm_title'), MHDEV_Sprintf($this->registry->get('lang')->_('banned_pm'), array(
			    		'%expire'	=> $date->datetime($form->getValue('expire_date')),
			    		'%reason'	=> $form->getValue('reason')
			    	)));
					
			    	$this->_helper->FlashMessenger($this->registry->get('lang')->_('flash_post_banned'));
					$this->_redirect('/posts/' . $resPost[0]['thread_uid'] . '.html#row1' . $resPost[0]['uid']);
				}
			}
			else
			{
				$form->populate($postData);
			}
		}
		
		$this->view->notice = $notice;
		$this->view->form = $form;
	}
}
?>